#include "userinfowidget.h"

#include <QGridLayout>

UserInfoWidget::UserInfoWidget(const model::UserInfo &userInfo, QWidget* parent)
    :QDialog(parent), userInfo(userInfo)
{
    // 初始化必要属性
    this->setWindowTitle("用户信息");
    this->setWindowIcon(QIcon(":/resource/image/logo.png"));
    this->setFixedSize(QSize(400, 350));
    this->setAttribute(Qt::WA_DeleteOnClose);

    QGridLayout* layout = new QGridLayout();
    layout->setSpacing(10);
    layout->setContentsMargins(20, 0, 20, 0);
    layout->setAlignment(Qt::AlignCenter);
    this->setLayout(layout);

    // 头像
    avatarBtn = new QPushButton();
    avatarBtn->setFixedSize(QSize(100, 100));
    avatarBtn->setIcon(userInfo.avatar);
    avatarBtn->setIconSize(QSize(100, 100));

    // 序号
    QString tagStyle = "QLabel { font-size: 15px; font-weight:600; color:rgb(200, 200, 200); }";
    QString labelStyle = "QLabel { font-size: 15px; font-weight:400; color:rgb(200, 200, 200); }";

    idTag = new QLabel();
    idTag->setText("序号");
    idTag->setStyleSheet(tagStyle);

    idLabel = new QLabel();
    idLabel->setText(userInfo.userId);
    idLabel->setStyleSheet(tagStyle);

    // 昵称
    nameTag = new QLabel();
    nameTag->setText("昵称");
    nameTag->setStyleSheet(tagStyle);

    nameLabel = new QLabel();
    nameLabel->setText(userInfo.nickname);
    nameLabel->setStyleSheet(tagStyle);

    // 电话
    phoneTag = new QLabel();
    phoneTag->setText("电话");
    phoneTag->setStyleSheet(tagStyle);

    phoneLabel = new QLabel();
    phoneLabel->setText(userInfo.phone);
    phoneLabel->setStyleSheet(tagStyle);

    // 按钮
    QString btnStyle = "QPushButton { font-size:15px; border:none; background-color:rgb(1, 1, 1); border-radius:2px }";
    btnStyle += "QPushButton:hover { background-color:rgb(50, 50, 50); }";
    btnStyle += "QPushButton:pressed { background-color:rgb(150, 150, 150); }";
    QSize btnSize(75, 30);

    addFriendBtn = new QPushButton();
    addFriendBtn->setFixedSize(btnSize);
    addFriendBtn->setText("添加好友");
    addFriendBtn->setStyleSheet(btnStyle);

    sendMsgBtn = new QPushButton();
    sendMsgBtn->setFixedSize(btnSize);
    sendMsgBtn->setText("发送消息");
    sendMsgBtn->setStyleSheet(btnStyle);

    deleteFriendBtn = new QPushButton();
    deleteFriendBtn->setFixedSize(btnSize);
    deleteFriendBtn->setText("删除好友");
    deleteFriendBtn->setStyleSheet(btnStyle);

    // 添加到布局管理器中
    layout->addWidget(avatarBtn, 0, 0, 3, 1);
    layout->addWidget(idTag, 0, 1, 1, 1);
    layout->addWidget(idLabel, 0, 2, 1, 1);
    layout->addWidget(nameTag, 1, 1, 1, 1);
    layout->addWidget(nameLabel, 1, 2, 1, 1);
    layout->addWidget(phoneTag, 2, 1, 1, 1);
    layout->addWidget(phoneLabel, 2, 2, 1, 1);
    layout->addWidget(addFriendBtn, 3, 0, 1, 1);
    layout->addWidget(sendMsgBtn, 3, 1, 1, 1);
    layout->addWidget(deleteFriendBtn, 3, 2, 1, 1);

    model::DataCenter* dataCenter = model::DataCenter::getInstance();
    model::UserInfo* myFriend = dataCenter->findUserInfoById(userInfo.userId);
    if(myFriend == nullptr) {
        // don't have this friend
        sendMsgBtn->setEnabled(false);
        deleteFriendBtn->setEnabled(false);
    } else {
        // have this friend
        addFriendBtn->setEnabled(false);
    }

    initSignalSlot();
}

void UserInfoWidget::initSignalSlot()
{
    connect(sendMsgBtn, &QPushButton::clicked, this, [=](){
        MainWidget* mainWidget = MainWidget::getInstance();
        mainWidget->switchToSession(userInfo.userId);
        this->close();
    });

    connect(deleteFriendBtn, &QPushButton::clicked, this, [=](){
        auto result = QMessageBox::warning(nullptr, "确认删除", "确认删除当前好友?", QMessageBox::Ok | QMessageBox::Cancel);
        if(result != QMessageBox::Ok) {
            return;
        }
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        dataCenter->deleteFriendAsync(userInfo.userId);
    });

    connect(addFriendBtn, &QPushButton::clicked, this, [=](){
        model::DataCenter* dataCenter = model::DataCenter::getInstance();
        dataCenter->sendFriendApplyAsync(userInfo.userId);
    });
}

